﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReviewCurveApp.Classes;
using ReviewCurveApp.Contorller;

namespace ReviewCurveApp
{
    public partial class FrmAddUnit : Form
    {
        public FrmAddUnit()
        {
            InitializeComponent();
        }
        
        Unit unit = new Unit();

        Subject _Subject = new Subject();

        UnitsManager unitsManager = new UnitsManager();

        SubjectsManager subjectsManager = new SubjectsManager();

        private void FrmAddUnit_Load(object sender, EventArgs e)
        {
            
        }



        private void button_GenerateTasks_Click(object sender, EventArgs e)
        {



          
            unit.ReAssign(dateTimePicker1.Value);

            GenerateTasks(unit);
        }

        private void GenerateTasks(Unit unit)
        {
            listView1.Items.Clear();
           
            for (int i = 0; i < unit.Tasks.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                Task task=unit.Tasks[i];
                item.Text = task.Sequnce.ToString();
                item.SubItems.Add(unit.Name+"任务"+(i+1).ToString());
                item.SubItems.Add(task.ScheduledTime.Value.ToShortDateString() + "  " + task.ScheduledTime.Value.ToShortTimeString());
                listView1.Items.Add(item);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            unit.Name = txt_ItemName.Text;
            
            unit.Description = txt_Description.Text;

            unit.CreateDateTime = DateTime.Now;

            unit.ScheduledDateTime = dateTimePicker1.Value;

            unit.SubjectId = _Subject.Id ;

            unit.Status = UnitStatus.Initiled;

            unit.Sequence = subjectsManager.GetNewUnitSequence(_Subject.Id);

            if (!unitsManager.AddUnit(unit))
            {
                MessageBox.Show("Adding failed !");
                return;
                
            }
            
            MessageBox.Show("Adding Successed !");

            this.DialogResult = DialogResult.OK;
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }



        internal void SetSubject(Subject subject)
        {
            _Subject = subject;
            label_Subject.Text = _Subject.Name;
        }
    }
}
